<?php

namespace builder\test\controllers;

use builder\base\BaseController;
use builder\tab\TabBuilder;
use builder\table\TableBuilder;

//Tab Builder Example
class TabController extends BaseController
{
    public $actionVerbs = ['index' => ['GET', 'POST'], 'ajax' => ['GET', 'POST']];
    public $publicActions = ['index', 'ajax'];

    public function actionAjax()
    {
        return $this->asOk('Success');
    }

    // Example
    public function actionIndex()
    {
        $tableBuilder1 = TableBuilder::instance(['id' => 'Tab_index_Table1']);
        $tableBuilder2 = TableBuilder::instance(['id' => 'Tab_index_Table2']);
        if ($this->isAjax) {
            if ($tableBuilder1->builderId == $tableBuilder1->id) {
                $tableBuilder1->setQuery([
                    ['id' => 1, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 2, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 3, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 4, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 5, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 6, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 7, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 8, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 9, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                    ['id' => 10, 'name' => 'Tome', 'age' => '13岁', 'class' => 'Grade 1'],
                ])
                    ->setColumns([
                        'id',
                        'name',
                        'age',
                        'class',
                    ])
                    ->setPage(false);

                return $tableBuilder1->render();
            } elseif ($tableBuilder2->builderId == $tableBuilder2->id) {
                $tableBuilder2->setQuery([
                    ['id' => 11, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 12, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 13, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 14, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 15, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 16, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 17, 'name' => 'Merry', 'age' => '23岁', 'class' => 'Grade 1'],
                    ['id' => 18, 'name' => 'Merry', 'age' => '33岁', 'class' => 'Grade 1'],
                    ['id' => 19, 'name' => 'Merry', 'age' => '33岁', 'class' => 'Grade 1'],
                    ['id' => 20, 'name' => 'Merry', 'age' => '33岁', 'class' => 'Grade 1'],
                ])
                    ->setColumns([
                        'id',
                        'name',
                        'age',
                        'class',
                    ])
                    ->setPage(false);
                return $tableBuilder2->render();
            }

        } else {
            $tableBuilder1->setTableHead([
                $this->tableHead->field('id')->title('ID'),
                $this->tableHead->field('name')->title('Name'),
                $this->tableHead->field('age')->title('Age'),
                $this->tableHead->field('class')->title('Class'),
                $this->tableHead->toolbar()->title('工具'),
            ])
            ->setAutoRequest(false)
            ->setRowOperation([
                $this->rowOperation->ajax()->title('删除')->route('ymtest/tab/ajax'),
            ])
            ->setToolbarPrint()
            ->setToolbarRefresh();

            $tableBuilder2->setTableHead([
                $this->tableHead->field('id')->title('ID'),
                $this->tableHead->field('name')->title('Name'),
                $this->tableHead->field('age')->title('Age'),
                $this->tableHead->field('class')->title('Class'),
                $this->tableHead->toolbar()->title('工具'),
            ])
                ->setAutoRequest(false)
                ->setRowOperation([
                    $this->rowOperation->ajax()->title('删除')->route('ymtest/tab/ajax'),
                ])
                ->setToolbarPrint()
                ->setToolbarRefresh();

            $tabBuilder = TabBuilder::instance(['id' => 'Tab_Index']);
            $tabBuilder->setPartial(!!@$this->get['_partial'])
                ->setTitle('Tab Demo')
                ->setTheme(@$this->get['theme'] ?: 'brief') //column card brief
                ->setTabItem([
                    '表格1' => $this->tabItem->builder($tableBuilder1)->selected()->switchReload(),
                    '表格2' => $this->tabItem->builder($tableBuilder2)->switchReload(),
                ]);

            return $tabBuilder->render();
        }
    }
}